package com.gw.yunke.service.impl;

import com.gw.yunke.dao.SectionMapper;
import com.gw.yunke.domain.Part;
import com.gw.yunke.domain.Section;
import com.gw.yunke.domain.SectionExample;
import com.gw.yunke.domain.Video;
import com.gw.yunke.domain.dto.PartResp;
import com.gw.yunke.domain.dto.SectionResp;
import com.gw.yunke.service.IPartService;
import com.gw.yunke.service.IPartVideoService;
import com.gw.yunke.service.ISectionService;
import com.gw.yunke.service.IVideoService;

import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

@Service
public class SectionServiceImpl implements ISectionService {

    private static final org.slf4j.Logger log = LoggerFactory.getLogger(SectionServiceImpl.class);

    @Autowired
    private SectionMapper sectionMapper;
    @Autowired
    private IPartService partService;
    @Autowired
    private IPartVideoService partVideoService;
    @Autowired
    private IVideoService videoService;

    //@CacheEvict(value = "sections",key = "#section.courseId")
    @Override
    public long createOrUpdateSection(Section section) {
        if(section.getId() == null){
            return sectionMapper.insertSelective(section);
        }else{
            return sectionMapper.updateByPrimaryKeySelective(section);
        }

    }

    //@Cacheable(value = "sections",key = "courseId")
    @Override
    public List<Section> getSectionsByCourseId(Long courseId) {
        SectionExample sectionExample = new SectionExample();
        SectionExample.Criteria criteria = sectionExample.createCriteria();
        criteria.andCourseIdEqualTo(courseId);
        sectionExample.setOrderByClause("section_order");
        return sectionMapper.selectByExample(sectionExample);
    }


    @Override
    public List<SectionResp> getSectionRespsByCourseId(Long courseId){
        List<SectionResp> sectionRespList = new ArrayList<>();
        List<Section> sectionList = getSectionsByCourseId(courseId);
        for(Section section : sectionList){
            SectionResp sectionResp = new SectionResp();
            sectionResp.setCourseId(courseId);
            sectionResp.setId(section.getId());
            sectionResp.setName(section.getName());
            section.setCreateTime(section.getCreateTime());
            sectionResp.setUpdateTime(section.getUpdateTime());
            sectionResp.setDescription(section.getDescription());
            sectionResp.setSectionOrder(section.getSectionOrder());

            List<Part> partList = partService.getPartsBySectionId(section.getId());
            List<PartResp> partRespList = new ArrayList<>();
            for(Part part : partList){
                PartResp partResp = new PartResp();
                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                partResp.setCreateTime(format.format(part.getCreateTime()));
                partResp.setName(part.getName());
                partResp.setPart_order(part.getPartOrder());
                partResp.setSectionId(part.getSectionId());
                partResp.setId(part.getId());
                long videoId = partVideoService.getVideoIdByPartId(part.getId());
                log.info("videoId= "+videoId);
                Video video = videoService.getVideoById(videoId);
                if(video != null) {
                    partResp.setVideoId(videoId);
                }else{
                    log.info("当前小节没有视频 partId="+part.getId());
                }
                partRespList.add(partResp);

            }

            sectionResp.setPartList(partRespList);
            sectionRespList.add(sectionResp);
        }
        return sectionRespList;
    }
}
